package cn.edu.njust.controller;

import cn.edu.njust.entity.HitCourse;
import cn.edu.njust.entity.Login;
import cn.edu.njust.entity.Page;
import cn.edu.njust.service.CourseService;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.util.ArrayList;

@WebServlet(name = "CourseController", value = "/CourseController")
public class CourseController extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//        response.getWriter().write("This is the Course Page!");
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=utf-8");
        String operation = request.getParameter("operation");
        if(operation == null || "".equals(operation)){
            operation = "all";
        }
        if(operation.equals("all")){                //显示全部课程
            HttpSession session = request.getSession();
            int isClicked;
            String _currentPage = request.getParameter("currentPage");
            String _isClicked = request.getParameter("isClicked");
            if (_isClicked == null || "".equals(_isClicked)) {
                isClicked = 0;
            } else {
                isClicked = Integer.parseInt(_isClicked);
            }
            String t = request.getParameter("tag");//获取标记  1为cName 2为cNum
            int tag = 0;
            Integer type = (Integer) session.getAttribute("type");
            if (type == null) {
                session.setAttribute("type", 0);
                type = 0;
            }
            if (t != null)
                tag = Integer.parseInt(t);
            CourseService cs = new CourseService();
            if (tag == 0) {
                type = 0;
                session.setAttribute("type",0);
            }
            if (tag == 1) {//操作课程名的排序
                if (type != 1 && type != 2) {
                    type = 1;
                    session.setAttribute("type", 1);
                }
                if (isClicked == 1) {
                    if (type == 1) {
                        type = 2;
                        session.setAttribute("type", 2);
                    } else if (type == 2) {
                        type = 1;
                        session.setAttribute("type", 1);
                    }
                }
            }
            if (tag == 2) {//操作选课人数的排序
                if (type != 3 && type != 4) {
                    type = 3;
                    session.setAttribute("type", 3);
                }
                if (isClicked == 1) {
                    if (type == 3) {
                        type = 4;
                        session.setAttribute("type", 4);
                    } else if (type == 4) {
                        type = 3;
                        session.setAttribute("type", 3);
                    }
                }
            }
            Page page = cs.selectByPage(type, _currentPage);
            request.setAttribute("page", page);
            request.getRequestDispatcher("allCourse.jsp").forward(request, response);
        }
        else if(operation.equals("add")){
            String cName = request.getParameter("cName");
            String cNum = request.getParameter("cNum");
            String cType = request.getParameter("cType");
            CourseService cs = new CourseService();
            int b = cs.add(cName,cNum,cType);
            if(b == 1){          //如果添加操作执行成功，跳转到allCourse显示课程
                response.sendRedirect("CourseController?operation=all");
//            request.getRequestDispatcher("/CourseController").forward(request,response);
            }else {
                if(b == 2)
                    request.setAttribute("ModErr","存在重复课程！");
                else if(b == 0)
                    request.setAttribute("ModErr","数据库操作失败！");
                request.getRequestDispatcher("CourseFailure.jsp").forward(request,response);
            }
        }
        else if (operation.equals("del")) {      //删除操作
            String tag = request.getParameter("tag");
            CourseService cs = new CourseService();
            boolean b = cs.sub(tag);
            if (b) {
                request.getRequestDispatcher("delCourse.jsp").forward(request,response);
//                response.sendRedirect("delCourse.jsp");
            } else {
                request.setAttribute("ModErr", "数据库操作失败！");
                request.getRequestDispatcher("CourseFailure.jsp").forward(request, response);
            }
        }
        else if(operation.equals("hitC")){//显示已选择课程
            CourseService cs = new CourseService();

            HttpSession session = request.getSession();
            Login user = (Login) session.getAttribute("user");
            ArrayList<HitCourse> list = cs.selectAllHit(user.getId());
            request.setAttribute("AllCourseHit",list);
            //转发
            request.getRequestDispatcher("/hitCourse.jsp").forward(request,response);

        }else if(operation.equals("hit")){//选课逻辑
            HttpSession session = request.getSession();
            Login user = (Login) session.getAttribute("user");//获取当前用户
            int uId = user.getId();
            String tag = request.getParameter("tag");
            CourseService cs = new CourseService();
            HitCourse b = cs.HitCourse(uId, tag);
            if (b == null) {               //操作成功
                response.sendRedirect("CourseController?operation=hitC");
            } else {                    //操作失败
                if(b.getuId() == 0){
                    int cId = b.getcId();
                    request.setAttribute("ModErr", "在"+cId+"处的数据库操作失败！");
                }else
                request.setAttribute("ModErr", "课程【"+b.getcName()+"】已经选过，选课失败!");
                request.getRequestDispatcher("CourseFailure.jsp").forward(request, response);
            }
        }else if(operation.equals("exit")){//处理退课请求
            HttpSession session = request.getSession();
            Login user = (Login) session.getAttribute("user");//获取当前用户
            int uId = user.getId();
            String tag = request.getParameter("tag");
            CourseService cs = new CourseService();
            int b = cs.ExitCourse(uId,tag);
            if (b != 0) {               //操作成功
                response.sendRedirect("CourseController?operation=hitC");
            } else {                    //操作失败
                request.setAttribute("ModErr", "数据库操作失败！");
                request.getRequestDispatcher("CourseFailure.jsp").forward(request, response);
            }
        }
        else{
            request.setAttribute("ModErr","请求非法！");
            request.getRequestDispatcher("CourseFailure.jsp").forward(request,response);
        }
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request,response);
    }
}
